import { Injectable } from '@angular/core'
import { HttpClient, HttpHeaders } from '@angular/common/http'
import { Observable, of, throwError } from 'rxjs'
import { catchError, map, mergeMap, mergeAll } from 'rxjs/operators'
import { COMB_URL } from 'app/utils/contraints'
import { BatchImportService } from './batch-import.service'
import { IBathImportPagination, ISampleReportPagination } from '../../shared/interfaces/batch-import'
import { preProcessParams, encodeParams } from '@shared/decorators'
import { IGeneralResponse } from '@shared/interfaces/response'

@Injectable()
export class BatchImportAlinterService implements BatchImportService {
  constructor(private http: HttpClient) {}

  @preProcessParams()
  @encodeParams()
  getList(params: any, str?: string): Observable<IBathImportPagination> {
    const url = `${COMB_URL}/camel/import/file/histories?${str}`
    return this.http.get<IBathImportPagination>(url).pipe(
      map(resp => {
        return resp
      }),
      catchError(err => throwError(err)),
    )
  }

  // 下载最新的批导模版
  downSample(params: any, str?: string) {
    const url = `${COMB_URL}/camel/import/samples`
    return this.http.get<ISampleReportPagination>(url,
    {
      headers: {
        'X-Platform': 'sp.everonet'
      },
    }
      ).pipe(
      map(resp => {
        return resp
      }),
      catchError(err => throwError(err)),
    )
  }

  // 上传批导模版
  uploadSample(params: any, str?: string) {
    const url = `${COMB_URL}/camel/import/samples`
    return this.http.post<any>(url,
      params,
      {
        headers: {
          'X-Platform': 'sp.everonet'
        },
      })
  }

  // 批导模版的历史记录
  @preProcessParams()
  @encodeParams()
  sampleHistory(params: any, str?: string) {
    const url = `${COMB_URL}/camel/import/sample/histories?${str}`
    return this.http.get<IBathImportPagination>(url).pipe(
      map(resp => {
        return resp
      }),
      catchError(err => throwError(err)),
    )
  }

  // 获取上传文件的链接
  uploadUrl(key) {
    const url = `${COMB_URL}/camel/import/upload-url/${key}`
    return this.http.get<any>(url).pipe(
      map(resp => {
        return resp
      }),
      catchError(err => throwError(err)),
    )
  }

  // 获取批导反馈文件
  getFeedBook(key) {
    const url = `${COMB_URL}/camel/import/file/feedback/${key}`
    return this.http.get<any>(url).pipe(
      map(resp => {
        return resp
      }),
      catchError(err => throwError(err)),
    )
  }


  // 上传批导文件
  uploadFile(params: any): Observable<any> {
    const url = `${COMB_URL}/camel/import/files`
    return this.http.post<any>(url, params, { observe: 'response' }).pipe(
      map(response => {
        const { status } = response
        if (status === 200) {
          return response.body
        }
        if (status === 202) {
          return response.body
        }
        return response.body
      }),
    )
  }

  //  上传批导文件到华为云


  uploadFileToHuaweiyun(url: string, data: any): Observable<any> {
    return this.http.put<any>(url, data, {
      observe: 'response',
  }).pipe(
      map(response => {
        const { status } = response
        if (status === 200) {
          return response.body
        }
        if (status === 202) {
          return response.body
        }
        return response.body
      }),
    )
  }
}
